﻿/// <reference path="../Templates/vshop/default/script/jquery-1.11.0.min.js" />


function vShop_RegionSelector(containerId, onSelected, defaultRegionText) {
    /// <param name="onSelected" type="function">选择地址后回调,包括两个参数，依次为址址和地址编码</param>

    init();
    var address = '';
    var code = 0;
    var country;

    function init() {
        if (!defaultRegionText)
            defaultRegionText = '请选择省市区';
        var text = '<select name="province"><option>请选择</option></select>\
        <select name="city"><option>请选择</option></select>\
        <select name="district"><option>请选择</option></select>';

        $('#' + containerId).append(text);
        initCountryData(function () {
            getRegin("province", 0, function () { bind(); });

        });

    }

    function initCountryData(callBack) {
        //var jsonFilePath = '../Utility/pages/region.js';
        //$.get(jsonFilePath, function (data) {
        //    country = JSON.parse(data);
            callBack();
        //});
    }

    function getRegin(regionType, parentRegionId, callback) {
        /// <param name="regionType" type="String">"province-省,city-市,district-区"</param>
        var text = '<option>请选择</option>';

        if (!parentRegionId) {
            parentRegionId = 0;
            address = '';
        }
        var noSub = false;


        var url = "RegionList?parentId=" + parentRegionId;
        
        $.get(url, function (data) {
            var regions = data;
            //var regions = getSubRegions(parentRegionId);
            if (regions && regions.length > 0) {
                $.each(regions, function (i, region) {
                    text += '<option value="' + region.RegionId + '">' + region.RegionName + '</option>';
                });
                $('#' + containerId + ' select[name="' + regionType + '"]').html(text);
            }
            else noSub = true;
            callback(noSub);

        });
    }

    function bind(noSub) {
        $('#' + containerId + ' select').unbind('change');
        $('select').selectmenu("refresh")
        $('#' + containerId + ' select').change(function () {
            var currentSe = $(this);
            var regionId = currentSe.val();
            var nextRegionSe = currentSe.next();
            var prevRegionSe = currentSe.prev();
            var nextRegionType = nextRegionSe ? $(nextRegionSe).attr('name') : '';
            address += $(this).html() + " ";
            if (nextRegionType) {
                code = $(this).attr('name');
                getRegin(nextRegionType, regionId, function (noSub) {
                    if (noSub) {
                        onSelected(address, code);
                        address = '';

                    }
                    else {
                    //    setTimeout(function () {
                    //        $(".btn-group").addClass('open');
                    //    }, 1);
                    }
                    bind(noSub);
                });
            }
            else {
                code = $(this).find('option').attr('name');
                onSelected(address, code);
                address = '';
            }
            
        });
        
    }
}